﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.order
{
    public partial class order_detail : Web.UI.ManagePage
    {
        private int id = 0;
        protected string dis = "none";
        protected Model.bs_good_order model = new Model.bs_good_order();

        protected void Page_Load(object sender, EventArgs e)
        {
            this.id = DTRequest.GetQueryInt("id");
            if (this.id == 0)
            {
                JscriptMsg("传输参数不正确！", "back");
                return;
            }
            if (!new BLL.bs_good_order().Exists(this.id))
            {
                JscriptMsg("记录不存在或已被删除！", "back");
                return;
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                ShowInfo(this.id);
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.bs_good_order bll = new BLL.bs_good_order();
            model = bll.GetModel(_id);
            BLL.bs_good_order_detail detalbll = new BLL.bs_good_order_detail();
            List<Model.bs_good_order_detail> detalList = detalbll.GetModelList("orderId='" + model.id + "' and " + Utils.getWhereByInfo(model.busnieseNum, model.appNum, model.templatesNum));
            //对 打印运单 操作所需信息 赋值（针对aojieer）
            //if (model.appNum.Equals("wxe68d84dbc2554452"))
            //{
            //    dis = "block";
            //}
            //string adressText = model.id + "," + model.isExpress + "_" + model.addressText + "_";
            //for (int i = 0; i < detalList.Count; i++)
            //{
            //    adressText += getGoodsMsg(detalList[i].goodsNum) + "|" + detalList[i].numbers + ",";
            //}
            //addressText.Text = adressText;
            //绑定商品列表
            this.rptList.DataSource = detalList;
            this.rptList.DataBind();
            //获得客户信息
            if (model.userNum != "")
            {
                Model.sf_user_info user_info = new BLL.sf_user_info().GetModelByNum(model.userNum, Utils.getWhereByInfo(model.busnieseNum, model.appNum, model.templatesNum));
                if (user_info != null)
                {
                    dlUserInfo.Visible = true;
                    lbUserName.Text = user_info.nickName;
                    lbUserNum.Text = user_info.userNum;
                }
            }
            #region 发货状态
            //如果还没发货，则确认发货启用，取消发货不启用
            if (model.expressStatus == SF.Common.DTEnums.ExpressStatusEnum.ToSend.ToString())
            {
                btnDoneSend.Visible = true;
                btnToSend.Visible = false;
            }
            else
            {
                btnDoneSend.Visible = false;
                btnToSend.Visible = true;
            }
            #endregion

            #region 结算状态
            if (model.balanceStatus == SF.Common.DTEnums.BalanceStatusEnum.DoneBalance.ToString())
            {
                btnBalanceStatus.Visible = false;
            }
            //else
            //{
            //    btnBalanceStatus.Text = "结算订单";
            //}
            #endregion

            #region 订单状态
            if (model.status == SF.Common.DTEnums.OrderStatusEnum.Normal.ToString())
            {
                btnOrderStatus.Text = "取消订单";
            }
            else
            {
                btnOrderStatus.Text = "激活订单";
            }
            #endregion
        }
        #endregion

        #region 获取商品信息
        public string GetGoodName(long goodId)
        {
            Model.bs_good_info goodInfo = new BLL.bs_good_info().GetModel(goodId);
            string res = goodInfo.name;
            return res;
        }
        #endregion

        /// <summary>
        /// 确认发货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDoneSend_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            BLL.bs_good_order orderBll = new BLL.bs_good_order();
            var order = orderBll.GetModel(this.id);
            //如果订单已取消，则不允许发货
            if (order.status == SF.Common.DTEnums.OrderStatusEnum.Cancel.ToString())
            {
                JscriptMsg(string.Format("订单{0}已取消，不允许发货", order.orderNum), "back");
                return;
            }
            //如果还没付款，则不允许发货
            if (order.payStatus != SF.Common.DTEnums.PayStatusEnum.DonePay.ToString())
            {
                JscriptMsg(string.Format("订单{0}未完成付款，请等待付款后操作", order.orderNum), "back");
                return;
            }
            //如果还没填写快递信息，则不允许发货
            if (String.IsNullOrWhiteSpace(order.expressName) || String.IsNullOrWhiteSpace(order.expressNum))
            {
                JscriptMsg(string.Format("订单{0}未填写快递信息，请填写后操作", order.orderNum), "back");
                return;
            }
            order.expressStatus = SF.Common.DTEnums.ExpressStatusEnum.DoneSend.ToString();
            order.expressTime = DateTime.Now;
            if (orderBll.Update(order))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}已发货", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}已发货", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
            else
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}发货操作失败", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}发货操作失败", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
        }

        /// <summary>
        /// 取消发货
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnToSend_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            BLL.bs_good_order orderBll = new BLL.bs_good_order();
            var order = orderBll.GetModel(this.id);
            //如果订单已结算，不允许取消发货
            if (order.balanceStatus == SF.Common.DTEnums.BalanceStatusEnum.DoneBalance.ToString())
            {
                JscriptMsg(string.Format("订单{0}已结算，不允许取消", order.orderNum), "back");
                return;
            }
            order.expressStatus = SF.Common.DTEnums.ExpressStatusEnum.ToSend.ToString();
            order.expressTime = null;
            if (orderBll.Update(order))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}已取消发货", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}已取消发货", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
            else
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}取消发货操作失败", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}取消发货操作失败", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOrderStatus_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            BLL.bs_good_order orderBll = new BLL.bs_good_order();
            var order = orderBll.GetModel(this.id);
            if (btnOrderStatus.Text == "取消订单")
            {
                //如果订单已经结算，则不允许取消
                if (order.balanceStatus == SF.Common.DTEnums.BalanceStatusEnum.DoneBalance.ToString())
                {
                    JscriptMsg(string.Format("订单{0}已结算，不允许取消", order.orderNum), "back");
                    return;
                }
                order.status = SF.Common.DTEnums.OrderStatusEnum.Cancel.ToString();
            }
            else if (btnOrderStatus.Text == "激活订单")
            {
                order.status = SF.Common.DTEnums.OrderStatusEnum.Normal.ToString();
            }
            if (orderBll.Update(order))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}状态已变更为{1}", order.orderNum, btnOrderStatus.Text)); //记录日志
                JscriptMsg(string.Format("订单{0}状态已变更为{1}", order.orderNum, btnOrderStatus.Text), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
            else
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}状态修改失败", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}状态修改失败", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
        }

        /// <summary>
        /// 修改结算状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBalanceStatus_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("order_list", DTEnums.ActionEnum.View.ToString()); //检查权限
            BLL.bs_good_order orderBll = new BLL.bs_good_order();
            var order = orderBll.GetModel(this.id);
            if (order.payStatus == SF.Common.DTEnums.PayStatusEnum.ToPay.ToString() || order.expressStatus == SF.Common.DTEnums.ExpressStatusEnum.ToSend.ToString())
            {
                JscriptMsg(string.Format("订单{0}未支付或未发货，不允许结算", order.orderNum), "back");
                return;
            }
            order.sysConfirmStatus = SF.Common.DTEnums.ConfirmStatusEnum.DoneConfirm.ToString();
            order.confirmTime = DateTime.Now;
            if (orderBll.Update(order))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}已结算", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}已结算", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
            else
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), string.Format("订单{0}状态修改失败", order.orderNum)); //记录日志
                JscriptMsg(string.Format("订单{0}结算失败", order.orderNum), Utils.CombUrlTxt("order_Detail.aspx", "action={0}&id={1}", "view", this.id.ToString()));
            }
        }
    }
}